Jx.declare('Jx.Base', {
	
	$extend: 'Jx.lib.Configurable',

	$actions: [

		'dispatch',

		'clearEvents',

		'suspendEvents',

		'resumeEvents',

		'on',

		'un'

	],

	event: null,

	eventDispatcherClass: 'Jx.event.Dispatcher',

	configOrder: [ 'listen' ],

	constructor: function(){

		//-- configure event
		this.event = Jx.instantiate( this.eventDispatcherClass, [ this ]);

		this.$super( arguments );

	},

	onConfigureListen: function(value){

		if (Jx.isObject(value)){

			this.on(value);

		}

	},

	onBeforeDestroy: function(){},

	onDestroy: function(){

		Jx.destroy(this.event);

		this.$super(arguments);

	},

	onDispatch: function(operation){

		var event = this.event;

		operation.returnValue = event.dispatch(

			event.createEvent( operation.name, operation.overrides )

		);

	},

	onClearEvents: function(operation){

		this.event.clear();

	},

	onSuspendEvents: function(operation){

		this.event.suspended !== false;

	},

	onResumeEvents: function(operation, suspend){

		this.event.suspended !== false;

	},

	onOn: function(operation){

		var event = this.event;

		event.listen.apply(event, operation.arguments);

	},

	onUn: function(operation){

		var event = this.event;

		event.unlisten.apply(event, operation.arguments);

	},

	allowDispatch: function(operation, name){

		operation.returnValue = undefined;

		operation.name = name;

		operation.overrides = {

			arguments: Array.prototype.slice.call(arguments, 2)

		};

		return this.isEventReady() && Jx.isString(name);

	},

	allowClearEvents: function(operation){

		return this.isEventReady(false);

	},

	allowSuspendEvents: function(operation){

		return this.isEventReady();

	},

	allowResumeEvents: function(operation){

		return this.isEventReady();

	},

	allowOn: function(operation, o){

		return this.isEventReady() &&

			Jx.isObject(o) || Jx.isString(o, true);

	},

	allowUn: function(operation, o){

		return this.isEventReady(false) &&

			Jx.isObject(o) || Jx.isString(o, true);

	},


	isEventReady: function(destroying){

		return Jx.instanceOf(this.event, this.eventDispatcherClass) && !this.isDestroyed( destroying );

	},

	destroy: function(){

		if (!this.isDestroyed()){

			this.dispatch('beforedestroy');

			this.onBeforeDestroy();

			this.dispatch('destroy');

		}

		return this.$super(arguments);


	}




});
